<?php

class PicasaUploader{
	
	private $_gp = NULL;
	
	private $_imageContent;
	
	private $_albumId;
	private $_filename;
	private $_photoName;
	private $_photoTags;
	
	function PicasaUploader($email = NULL, $password = NULL){
		
		require_once(dirname(__FILE__).'/../Zend/Loader.php');
		
		Zend_Loader::loadClass('Zend_Gdata_Photos');
		Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
		Zend_Loader::loadClass('Zend_Gdata_AuthSub');
		
		if($email != NULL && $password != NULL){
			$this->login($email, $password);
		}
		
		$this->_albumId = $this->getTodayAlbum();
	}
	
	
	
	function login($username, $password){
		
		$serviceName = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
		
		$serviceName = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
		$client = Zend_Gdata_ClientLogin::getHttpClient($username, $password, $serviceName);
		
		// update the second argument to be CompanyName-ProductName-Version
		$gp = new Zend_Gdata_Photos($client, "DEVXII-PICAKU-1.0");
		
		$this->_gp = $gp;
	}
	
	
	
	function getTodayAlbum(){
		$gp = $this->_gp;
		
		// We use the albumId of 'default' to indicate that we'd like to upload
		// this photo into the 'drop box'.  This drop box album is automatically 
		// created if it does not already exist.
		$albumDate = date("Y-m-d");
		
		if(0){
			
			$albumEntry = new Zend_Gdata_Photos_AlbumEntry();
			$albumEntry->setTitle($gp->newTitle($albumDate));
			$albumEntry->setSummary($gp->newSummary("This is an album of date ".$albumDate."."));
			$createdEntry = $gp->insertAlbumEntry($albumEntry);
			
			$albimId = $createdEntry->getGphotoId()->getText();
			
		}else{
			
			$albumId = '5413911315155142849';
			
		}
		
		return $albumId;
	}
	
	
	
	function _upload(){
		$gp = $this->_gp;
		
		
		$albumId = $this->getAlbumId();
		$filename = $this->getFileName();
		
		$photoName = $this->getPhotoName();
		$photoTags = $this->getPhotoTags();
		
		
		
		try {
	
	
			
			$fd = $gp->newMediaFileSource($filename);
			$fd->setContentType("image/jpeg");
			
			
			
			// Create a PhotoEntry
			$photoEntry = $gp->newPhotoEntry();
			
			$photoEntry->setMediaSource($fd);
			$photoEntry->setTitle($gp->newTitle($photoName));
			
			
			
			// add some tags
			$keywords = new Zend_Gdata_Media_Extension_MediaKeywords();
			$keywords->setText($photoTags);
			$photoEntry->mediaGroup = new Zend_Gdata_Media_Extension_MediaGroup();
			$photoEntry->mediaGroup->keywords = $keywords;
			
			
			
			// We use the AlbumQuery class to generate the URL for the album
			$albumQuery = $gp->newAlbumQuery();
			$albumQuery->setAlbumId($albumId);
			
			
			
			// We insert the photo, and the server returns the entry representing
			// that photo after it is uploaded
			$insertedEntry = $gp->insertPhotoEntry($photoEntry, $albumQuery->getQueryUrl());
			
			
				
		
			
			$this->_imageContent = $insertedEntry->getContent();
			return true;

		
		}catch (Zend_Gdata_App_HttpException $e) {
		
			
			echo "Error: " . $e->getMessage() . "<br />\n"; 
			exit();
			
		
		}catch (Zend_Gdata_App_HttpException $e) {
			
			
			echo "Error: " . $e->getMessage() . "<br />\n"; 
			exit();
			
		
		}
	
		
	}
	
	
	function upload($filename = NULL, $photoName = NULL, $photoTags = NULL){
		
		if($filename != NULL){
			$this->setFileName($filename);
		}
		
		if($photoName != NULL){
			$this->setPhotoName($photoName);
		}
		
		if($photoTags != NULL){
			$this->setPhotoTags($photoTags);
		}
		
		if($this->_upload()){
			return true;
		}else{
			return false;
		}
	}
	
	
	
	
	
	function getAlbumId(){
		return $this->_albumId;
	}
	
	function getContent(){
		return $this->_imageContent;
	}
	
	function setFileName($filename){
		$this->_filename = $filename;
	}
	
	function getFileName(){
		return $this->_filename;
	}
	
	function setPhotoName($photoName){
		$this->_photoName = $photoName;
	}
	
	function getPhotoName(){
		return $this->_photoName;
	}
	
	function setPhotoTags($photoTags){
		$this->_photoTags = $photoTags;
	}
	
	function getPhotoTags(){
		return $this->_photoTags;
	}
	
}